<template>
  <div>
    <!-- 工艺路线查询 -->
    <el-form :inline="true" :model="form" class="demo-form-inline">
      <el-form-item label="工艺路线编号：">
        <el-input v-model="form.code" placeholder="工艺路线编号"></el-input>
      </el-form-item>
      <el-form-item label="工艺路线名称：">
        <el-input v-model="form.name" placeholder="工艺路线名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="GetRouting">查询</el-button>
        <!-- 添加、编辑、删除、导入、导出 -->
        <el-button type="success" @click="dialogTableVisible = true"
          >新建</el-button
        >
        <el-dialog
          title="工艺路线（新建）"
          :visible.sync="dialogTableVisible"
          width="1500px"
        >
          <el-form :model="forms" :rules="rules" ref="forms">
            <el-form-item
              label="工艺路线编号"
              prop="code"
              :label-width="formLabelWidth"
            >
              <el-input v-model="forms.code" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item
              label="工艺路线名称"
              prop="name"
              :label-width="formLabelWidth"
            >
              <el-input v-model="forms.name" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogTableVisible = false">取 消</el-button>
            <el-button type="primary" @click="AddRouting('forms'), AddProcess"
              >确 定</el-button
            >
          </div>
          <br />
          <h2>工艺路线--工序列表</h2>
          <el-form ref="form2" :model="form2" label-width="80px">
            <el-form-item label="工艺路线">
              <el-input v-model="form2.name"></el-input>
            </el-form-item>
            <el-form-item label="类型">
              <el-select v-model="form2.typeId" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="工序">
              <el-select v-model="form2.Process_Name" placeholder="请选择">
                <el-option
                  v-for="item in options1"
                  :key="item.Process_Name"
                  :label="item.Process_Name"
                  :value="item.Process_Name"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="工艺路线">
              <el-select v-model="form2.routingName" placeholder="请选择">
                <el-option
                  v-for="item in options2"
                  :key="item.Name"
                  :label="item.Name"
                  :value="item.Name"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="报工数配比">
              <el-input v-model="form2.report"></el-input>
            </el-form-item>
          </el-form>
        </el-dialog>

        <el-button type="info" @click="GetRoutingById">编辑</el-button>
        <el-dialog
          title="工艺路线管理（编辑）"
          :visible.sync="dialogFormVisible"
        >
          <el-form :model="formUpd" :rules="rules1" ref="formUpd">
            <el-form-item
              label="工艺路线编号"
              prop="code"
              :label-width="formLabelWidth"
            >
              <el-input v-model="formUpd.code" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item
              label="工艺路线名称"
              prop="name"
              :label-width="formLabelWidth"
            >
              <el-input v-model="formUpd.name" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="UpdateRouting('formUpd')"
              >确 定</el-button
            >
          </div>
          <br />
          <h2>工艺路线--工序列表</h2>
          <el-form ref="form3" :model="form3" label-width="80px">
            <el-form-item label="工艺路线">
              <el-input v-model="form3.name"></el-input>
            </el-form-item>
            <el-form-item label="类型">
              <el-select v-model="form3.typeId" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="工序">
              <el-select v-model="form3.Process_Name" placeholder="请选择">
                <el-option
                  v-for="item in options1"
                  :key="item.Process_Name"
                  :label="item.Process_Name"
                  :value="item.Process_Name"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="工艺路线">
              <el-select v-model="form3.routingName" placeholder="请选择">
                <el-option
                  v-for="item in options2"
                  :key="item.Name"
                  :label="item.Name"
                  :value="item.Name"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="报工数配比">
              <el-input v-model="form3.report"></el-input>
            </el-form-item>
          </el-form>
        </el-dialog>

        <el-button type="warning" @click="DeleleRouting">删除</el-button>
        <el-button type="danger" @click="GetRouting">导入</el-button>
        <el-button type="danger" @click="GetRouting">导出</el-button>
      </el-form-item>
    </el-form>
    <!-- 工艺路线信息 -->
    <el-table
      ref="multipleTable"
      :data="table"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column prop="Id"> </el-table-column>
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="Code" label="工艺路线编号">
        <template slot-scope="scope">
          <span @click="GetProcess(scope.row.Id)">{{ scope.row.Code }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="Name" label="工艺路线名称"> </el-table-column>
      <el-table-column prop="CreateDate" label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.CreateDate.replace("T", " ").substr(0, 19) }}
        </template>
      </el-table-column>
      <el-table-column prop="CreatorId" label="创建人"> </el-table-column>
      <el-table-column prop="Updator" label="修改人"> </el-table-column>
      <el-table-column prop="UpdateDate" label="修改时间">
        <template slot-scope="scope">
          {{ scope.row.UpdateDate.replace("T", " ").substr(0, 19) }}
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页控件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.pageIndex"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount"
    >
    </el-pagination>
    <!-- 工艺路线--工序列表 -->
    <el-table
      ref="multipleTable"
      :data="table1"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="Name" label="工艺路线"> </el-table-column>
      <el-table-column prop="TypeId" label="类型">
        <template slot-scope="scope">
          {{ scope.row.TypeId == 1 ? "工艺路线" : "工序" }}
        </template>
      </el-table-column>
      <el-table-column prop="ProcessName" label="工序"> </el-table-column>
      <el-table-column prop="RoutingName" label="工艺路线"> </el-table-column>
      <el-table-column prop="Report" label="报工数配比"> </el-table-column>
      <el-table-column prop="CreateTime" label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.CreateTime.replace("T", " ").substr(0, 19) }}
        </template>
      </el-table-column>
      <el-table-column prop="CreateId" label="创建人"> </el-table-column>
      <el-table-column prop="UpdateId" label="修改人"> </el-table-column>
      <el-table-column prop="UpdateTime" label="修改时间">
        <template slot-scope="scope">
          {{ scope.row.UpdateTime.replace("T", " ").substr(0, 19) }}
        </template></el-table-column
      >
    </el-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        code: "",
        name: "",
      },
      forms: {
        creatorId: sessionStorage.getItem("UserId"),
        updator: sessionStorage.getItem("UserId"),
        name: "",
        code: "",
        isDel: false,
      },
      formUpd: {
        name: "",
        code: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "工艺路线名称不能为空！",
            trigger: "blur",
          },
        ],
      },
      rules1: {
        name: [
          {
            required: true,
            message: "工艺路线名称不能为空！",
            trigger: "blur",
          },
        ],
      },
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      table: [], //工艺路线信息
      multipleTable: [], //选中的工艺路线信息
      page: {
        pageIndex: 1,
        pageSize: 10,
        pageCount: 0,
        totalCount: 0,
      },
      form2: {
        name: "",
        typeId: 0 || "",
        Process_Name: "",
        routingName: "",
        report: "",
        createTime: "",
        createId: "",
        updateTime: "",
        updateId: "",
      },
      form3: {
        name: "",
        typeId: 0 || "",
        Process_Name: "",
        routingName: "",
        report: "",
        createTime: "",
        createId: "",
        updateTime: "",
        updateId: "",
      },
      typeId: 0,
      options: [
        {
          value: "1",
          label: "工艺路线",
        },
        {
          value: "2",
          label: "工序",
        },
      ],
      options1: [],
      options2: [],
      table1: [], //工艺路线--工序列表
    };
  },
  methods: {
    //全选、全取消
    handleSelectionChange(val) {
      this.multipleTable = val;
    },
    //每页显示
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.GetRouting();
    },
    //当前页
    handleCurrentChange(val) {
      this.page.pageIndex = val;
      this.GetRouting();
    },
    //分页、显示、查询工艺路线信息
    GetRouting() {
      this.axios
        .get("http://localhost:5255/api/Routing/GetRouting", {
          params: {
            pageIndex: this.page.pageIndex,
            pageSize: this.page.pageSize,
            code: this.form.code,
            name: this.form.name,
          },
        })
        .then((res) => {
          this.table = res.data.Data.List;
          this.page.totalCount = res.data.Data.TotalCount;
          this.page.pageCount = res.data.Data.PageCount;
        });
    },
    //添加工艺路线信息
    AddRouting(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios
            .post("http://localhost:5255/api/Routing/AddRouting", this.forms)
            .then((res) => {
              if (res.data) {
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
                this.dialogTableVisible = false;
                this.GetRouting();
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //删除工艺路线信息
    DeleleRouting() {
      if (this.multipleTable.length == 0) {
        this.$message({
          message: "请选择要删除的信息",
          type: "warning",
        });
        return;
      }
      let id = 0;
      this.multipleTable.forEach((item) => {
        id = item.Id;
      });
      this.$confirm("是否删除该信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .delete("http://localhost:5255/api/Routing/DeleleRouting?id=" + id)
            .then((res) => {
              if (res.data) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.GetRouting();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //反填工艺路线信息
    GetRoutingById() {
      if (this.multipleTable.length == 0) {
        this.$message({
          message: "请选择要编辑的信息",
          type: "warning",
        });
        return;
      }
      this.dialogFormVisible = true;
      let id = 0;
      this.multipleTable.forEach((item) => {
        id = item.Id;
      });
      this.axios
        .get("http://localhost:5255/api/Routing/GetRoutingById?id=" + id)
        .then((res) => {
          this.formUpd.name = res.data.Data.Name;
          this.formUpd.code = res.data.Data.Code;
        });
    },
    //修改工艺路线信息
    UpdateRouting(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios
            .put("http://localhost:5255/api/Routing/UpdateRouting", {
              id: this.multipleTable[0].Id,
              name: this.formUpd.name,
              code: this.formUpd.code,
              creatorId: this.multipleTable[0].CreatorId,
              createDate: this.multipleTable[0].CreateDate,
              updator: sessionStorage.getItem("UserId"),
              isDel: this.multipleTable[0].IsDel,
            })
            .then((res) => {
              if (res.data) {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.dialogFormVisible = false;
                this.GetRouting();
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //获取工艺路线-工序列表信息
    GetProcess() {
      this.axios
        .get("http://localhost:5255/api/Routing/GetProcess?id=" + 1)
        .then((res) => {
          this.table1 = res.data.Data;
        });
    },
    //显示工艺路线信息（用于下拉框显示）
    GetRoutings() {
      this.axios
        .get("http://localhost:5255/api/Routing/GetRoutings")
        .then((res) => {
          this.options2 = res.data.Data;
        });
    },
    //添加工艺路线-工序列表信息
    AddProcess() {
      this.axios
        .post("http://localhost:5255/api/Routing/AddProcess", this.form2)
        .then((res) => {
          if (res.data) {
            this.$message({
              message: "添加成功",
              type: "success",
            });
            this.dialogTableVisible = false;
            this.GetProcess();
          }
        });
    },
    //工序单表显示数据
    GetResultShows() {
      this.axios.get("http://localhost:5255/api/ProcEs").then((res) => {
        this.options1 = res.data.Data;
      });
    },
  },
  created() {
    this.GetRouting();
    this.GetProcess();
    this.GetRoutings();
    this.GetResultShows();
  },
};
</script>